package com.gy.mbaselibrary.utils;


import android.os.Handler;
import android.os.Message;

/**
 * 创建者：高业
 * 邮箱：15234150168@163.com
 * 时间：2018/2/6
 * 描述：
 * 修改人：
 * 修改时间：2018/2/6
 * 修改备注：
 */

public class TimeUtil {
    private final int MWHAT = 0x12345678;
    private long shutTime;
    private long allTime;
    private long cishu;
    private TimeMesListener listener;



    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == MWHAT){
                if (allTime != -1l){
                    cishu++;
                    long shengtime = allTime - cishu * shutTime;
                    if (shengtime <= 0l){
                        listener.timeFinishListener();
                    }else {
                        listener.timeMesListener(allTime,shengtime,cishu);
                        sendEmptyMessageDelayed(MWHAT,shutTime);
                    }

                }
            }
        }
    };

    /*public static TimeUtil getInstanc(){
        if (instance == null)
            instance = new TimeUtil();
        return instance;
    }*/

    public void excuse(long shutTime,TimeMesListener listener){
        excuse(shutTime,-1l,listener);
    }

    public void excuse(long shutTime,long allTime,TimeMesListener listener){
        handler.removeMessages(MWHAT);
        this.shutTime = shutTime;
        this.listener = listener;
        this.allTime = allTime;
        this.cishu = 0;

        handler.sendEmptyMessageDelayed(MWHAT,shutTime);
        listener.timeStartListener(allTime);
    }

    public void pause(){
        handler.removeMessages(MWHAT);
    }

    public void onDestory(){
        handler.removeMessages(MWHAT);
    }


    public interface TimeMesListener{
        public void timeMesListener(long allTime, long shengTime, long cishu);
        public void timeStartListener(long allTime);
        public void timeFinishListener();
    }

    public static long getMiaoLong(){
        return 1000l;
    }

    public static long getFenLong(){
        return 60l * getMiaoLong();
    }

    public static long getHourLong(){
        return 60l * getFenLong();
    }

    public static long getDayLong(){
        return 24l * getHourLong();
    }

    public static String timeToStr(long time){
        if (time < getFenLong()) {
            return time / 1000l + "秒";
        }else{
            return time / getFenLong() + "分钟";
        }
    }


    public static String formatDateTime(long mss) {
        String DateTimes = null;
        mss = mss / 1000l;
        long days = mss / ( 60l * 60l * 24l);
        long hours = (mss % ( 60l * 60l * 24l)) / (60l * 60l);
        long minutes = (mss % ( 60l * 60l)) /60l;
        long seconds = mss % 60l;
        if(days>0){
            DateTimes= days + "天" + hours + "小时" + minutes + "分钟"
                    + seconds + "秒";
        }else if(hours>0){
            DateTimes=hours + "小时" + minutes + "分钟"
                    + seconds + "秒";
        }else if(minutes>0){
            DateTimes=minutes + "分钟"
                    + seconds + "秒";
        }else{
            DateTimes=seconds + "秒";
        }
        return DateTimes;
    }

    public static String timeToStr2(long time){
        if (time < getFenLong()) {
            return time / 1000l + "秒";
        }else if (time < getHourLong()) {
            return time / getFenLong() + "分钟";
        }else{
            long sheng = time % getHourLong();
            if ( sheng / getFenLong() == 0) {
                return time / getHourLong() + "小时";
            }else{
                return time / getHourLong() + "小时" + sheng / getFenLong() + "分钟";
            }

        }
    }
}
